package com.smart.service.impl;

import java.util.List;
import java.util.ArrayList;
import com.smart.entity.SsWork;
import com.smart.util.DateUtil;
import com.smart.entity.SysUser;
import javax.annotation.Resource;
import com.smart.entity.WorkOrder;
import com.smart.entity.WorkOrderLog;
import com.smart.entity.SysMainRecords;
import com.smart.service.SsWorkService;
import com.smart.mapper.WorkOrderMapper;
import com.smart.common.util.SysUserUtils;
import com.smart.service.WorkOrderService;
import com.smart.service.MainRecordsService;
import com.smart.service.WorkOrderLogService;
import org.springframework.stereotype.Service;
import com.smart.service.InspectionRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lgb
 * @date 2020年7月1日 上午10:10:17
 * Copyright (C) 深圳工勘科技
 * @说明: 工单serviceImpl
 **/
@Service
public class WorkOrderServiceImpl extends BaseServiceImpl<WorkOrder,Integer>
        implements WorkOrderService{

	@Resource
	private SsWorkService workService;
	@Resource
	private WorkOrderMapper workOrderMapper;
	@Resource
	private WorkOrderLogService orderLogService; 
	@Resource
	private MainRecordsService mainRecordsService;
	@Resource
	private InspectionRecordService inspectionRecordService;
	@Override
	public List<WorkOrder> findLogPage(int pageNum,int pageSize,int status){
		Integer count = workOrderMapper.findLogCount(status);
		List<WorkOrder> data = new ArrayList<>();
		if(count != 0){
			int start = (pageNum-1)*pageSize;
			int end   = pageNum * pageSize;
			data = workOrderMapper.findLogList(status,start,end);
		}
		return data;
	}
	@Override
	@Transactional(rollbackFor=RuntimeException.class)
	public String examine(int status,int workOrderId,String reason) {
		WorkOrder workOrder = super.findById(workOrderId);
		/**
		 *1新增工单修改状态 2修改作业状态
		 **/
		switch (workOrder.getType()) {
			case 0://表示作业记录
				SsWork ssWork = workService.findById(workOrder.getWorkId().longValue());
				ssWork.setStatus(status);
				ssWork.setReason(reason);
				workService.saveOrUpdate(ssWork);
				break;
			case 1:
				SysMainRecords sysMainRecords =
				mainRecordsService.findById(workOrder.getWorkId().longValue());
				sysMainRecords.setStatus(status);
				sysMainRecords.setReason(reason);
				mainRecordsService.saveOrUpdate(sysMainRecords);
				break;
		default:
			
			
			break;
		}
		SysUser userInfo = SysUserUtils.getSysUser();
		workOrder.setReason(reason);//建议 通过不必填 不通过必填
		workOrder.setStatus(status);
		workOrder.setExamineId(userInfo.getId().intValue());
		workOrder.setExamineName(userInfo.getNickName());
		super.saveOrUpdate(workOrder);
		WorkOrderLog orderLog = new WorkOrderLog();
		orderLog.setCreateId(workOrder.getCreateId());
		orderLog.setCreateName(workOrder.getCreateName());
		orderLog.setCreateTime(System.currentTimeMillis());
		orderLog.setExamineId(userInfo.getId().intValue());
		orderLog.setExamineName(userInfo.getNickName());
		orderLog.setReason(reason);
		orderLog.setStatus(status);
		orderLog.setOrderId(workOrder.getId());
		orderLogService.save(orderLog);
		return DateUtil.formatCurrentDateToLongStr();
	}
	/** 
	 * (non-Javadoc)
	 * @see: 根据工单id查询某个具体工作
	 **/
	@Override
	@Transactional(readOnly=true)
	public Object findWorkByOrderId(int orderId) {
		WorkOrder orderLog = super.findById(orderId);
		Object obj = null;
		switch (orderLog.getType()) {
			case 0://作业管理
				obj=workService.findById(orderLog.getWorkId().longValue());
				break;
			case 1://保养记录
				obj = mainRecordsService.findById(orderLog.getWorkId().longValue());
				break;
			case 2:
				obj = inspectionRecordService.getById(orderLog.getWorkId().longValue());
				break;
			default:
				break;
			}
		return obj;
	}

	
}